package com.example.bootdemo.handler;

import com.example.bootdemo.service.TbLampService;
import com.example.bootdemo.service.TbLampStatusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

@Component
public class ReceiverMessageHandler implements MessageHandler {
    @Autowired
    private TbLampService tbLampService ;

    @Autowired
    private TbLampStatusService tbLampStatusService ;
    @Override
    public void handleMessage(Message<?> message) throws MessagingException {
        MessageHeaders headers = message.getHeaders();
        String receivedTopicName = (String) headers.get("mqtt_receivedTopic");
        if("atguigu/iot/lamp/line".equals(receivedTopicName)) {
            System.out.println("接收到消息：" + message.getPayload());
            tbLampService.updateLampOnlineStatus(message.getPayload().toString()) ;        // 更新智能灯泡的上线状态
        }else if("atguigu/iot/lamp/device/status".equals(receivedTopicName)) {
            tbLampStatusService.saveDeviceStatus(message.getPayload().toString()) ;
        }
    }

}